<?php

class Application_Model_LogDb
{
    protected $_table_class;
    protected $_log_period;

    public function __construct()
    {
        $this->_table_class='Application_Model_DbTable_Log';
        
        $this->_log_period=3600*24*30;
        
        $this->emptyTrash();
    }
    
    /**
     * Returns an array with log objects
     *
     * @return array
     */
    public function fetch()
    {
        $logs=array();
        
        $log_tbl=new $this->_table_class();
        
        
        $log_rows=$log_tbl->fetchAll(null,array('tstamp DESC'));

        foreach ($log_rows as $row)
        {
            $log=new Application_Model_Log($row);
            
            $logs[]=$log;
        }
        
        return $logs;
    }
    
    /**
     * Returns an array with User Agents for today
     *
     * @return array
     */
    public function fetchTodayUA()
    {
        $stats=array();
        
        $log_tbl=new $this->_table_class();
        
        $select=$log_tbl->select()
                ->distinct()
                ->from(Application_Model_Log::DB_TABLE,'ua')
                ->where('tstamp>?',mktime(0,0,0))
                ->order('tstamp DESC')
                ->limit(100);
         
        $result=$select->query();
        $stat_rows=$result->fetchAll();

        foreach ($stat_rows as $row)
        {
            $stats[]=$row['ua'];
        }
        
        return $stats;
    }
    
    /**
     * Returns an array with referers for today
     *
     * @return array
     */
    public function fetchTodayReferers()
    {
        $stats=array();
        
        $log_tbl=new $this->_table_class();
        
        $select=$log_tbl->select()
                ->distinct()
                ->from(Application_Model_Log::DB_TABLE,'referer')
                ->where('tstamp>?',mktime(0,0,0))
                ->where('referer<>?','none')
                ->where('referer NOT LIKE ?',Zend_Registry::get('view')->siteUrl().'%')
                ->order('tstamp DESC')
                ->limit(100);
         
        $result=$select->query();
        $stat_rows=$result->fetchAll();

        foreach ($stat_rows as $row)
        {
            $stats[]=urldecode($row['referer']);
        }
        
        return $stats;
    }
    
    /**
     * Returns an array with IPs for today
     *
     * @return array
     */
    public function fetchTodayIP()
    {
        $stats=array();
        
        $log_tbl=new $this->_table_class();
        
        $select=$log_tbl->select()
                ->distinct()
                ->from(Application_Model_Log::DB_TABLE,'ip')
                ->where('tstamp>?',mktime(0,0,0))
                ->order('tstamp DESC')
                ->limit(100);
         
        $result=$select->query();
        $stat_rows=$result->fetchAll();

        foreach ($stat_rows as $row)
        {
            $stats[]=$row['ip'];
        }
        
        return $stats;
    }
    
    /**
     * Saves log data to database
     *
     * @param mixed $log
     * 
     * @return int
     */
    public function save($log)
    {
        if (is_object($log) && ($log instanceof Application_Model_Log))
        {
            $log=$log->toArray();
        }
        
        if (is_array($log))
        {
            $log_tbl=new $this->_table_class();
            
            if (!empty($log['id']))
            {
                return $log_tbl->update($log,array('id=?'=>$log['id']));
            } else 
            {
                return $log_tbl->insert($log);
            }
        }
    }
    
    /**
     * Deletes log from database
     *
     * @param mixed $log
     * @return int
     */
    public function delete($log)
    {
        if (is_object($log) && ($log instanceof Application_Model_Log))
        {
            $log=$log->toArray();
        }
        
        if (is_array($log) && !empty($log['id']))
        {
            $log_tbl=new $this->_table_class();

            return $log_tbl->delete(array('id=?'=>$log['id']));
        }
    }
    
    /**
     * Clear logs
     *
     * @return int
     */
    public function clear()
    {
        $log_tbl=new $this->_table_class();

        return $log_tbl->delete();
    }
    
    /**
     * Deletes log trash from database
     *
     * @return int
     */
    public function emptyTrash()
    {
        $log_tbl=new $this->_table_class();

        return $log_tbl->delete(array('tstamp<?'=>time()-$this->_log_period));
    }
    
    /**
     * Returns logs paginator
     *
     * @param int $page
     * @param int $limit
     * 
     * @return array
     */
    public function getLogsPage($page=1,$limit=10)
    {
        $logs=array();
        
        $db=Zend_Registry::get('db');
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()
                                                          ->from(Application_Model_Log::DB_TABLE)
                                                          ->order(array('tstamp DESC')));
        
        $rowCount = $db->select()->from(Application_Model_Log::DB_TABLE)
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns(new Zend_Db_Expr('COUNT(*) AS '.$db->quoteIdentifier(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN)));
    
        $adapter->setRowCount($rowCount);
        
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber((int) $page);
        $paginator->setItemCountPerPage($limit);
        
        if (count($paginator))
        {
            foreach ($paginator as $row)
            {
                $log=new Application_Model_Log($row);
            
                $logs[]=$log;
            }
        }
        
        return array($logs,$paginator);
    }
}

